/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.enums.DocumentKindEnum;
import com.icinfo.cloud.provider.punish.config.dto.ConfigFastCaseMattersDto;
import com.icinfo.cloud.provider.punish.config.dto.ConfigFastProcedureDto;
import com.icinfo.cloud.provider.punish.config.enums.FastCaseMattersTemplateTypeEnum;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigFastCaseMattersMapper;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigFastCaseMattersTemplateMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigBusiParamDocumentInfo;
import com.icinfo.cloud.provider.punish.config.model.ConfigFastCaseMatters;
import com.icinfo.cloud.provider.punish.config.model.ConfigFastCaseMattersTemplate;
import com.icinfo.cloud.provider.punish.config.model.ConfigFastProcedure;
import com.icinfo.cloud.provider.punish.config.service.IConfigBusiParamDocumentInfoService;
import com.icinfo.cloud.provider.punish.config.service.IConfigFastCaseMattersService;
import com.icinfo.cloud.provider.punish.config.utils.AuditDataUtil;
import com.icinfo.cloud.provider.punish.config.vo.*;
import com.icinfo.cloud.provider.punish.evideinv.enums.EvidenceOperateTypeEnum;
import com.icinfo.cloud.provider.punish.fastcase.enums.FastCaseEvidenceCollectOperateButtonEnum;
import com.icinfo.cloud.provider.punish.fastcase.vo.FastCaseEvidenceCollectOperateButtonInfoVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.CaseTypeEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseMainInfoVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 描述: 速结程序配置表 config_fast_case_matters 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2023年08月21日
 */
@Service
public class ConfigFastCaseMattersServiceImpl extends MyBatisServiceSupport implements IConfigFastCaseMattersService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigFastCaseMattersServiceImpl.class);

    @Resource
    private ConfigFastCaseMattersMapper configFastCaseMattersMapper;
    @Resource
    private ICaseMainInfoService caseMainInfoService;
    @Resource
    private ConfigFastCaseMattersTemplateMapper configFastCaseMattersTemplateMapper;
    @Resource
    private IConfigBusiParamDocumentInfoService configBusiParamDocumentInfoService;
   private static final List<String> FAST_CASE_EVIDENCE_LIST =Arrays.asList(DocumentKindEnum.DOC_KIND_DK2XCKYJCBL.getCode(),
           DocumentKindEnum.DOC_KIND_DK2XWBL.getCode(),
           DocumentKindEnum.DOC_KIND_DK2SFZM.getCode(),
           DocumentKindEnum.DOC_KIND_DK2KYBL.getCode(),
           DocumentKindEnum.DOC_KIND_DK2XCSYT.getCode(),
           DocumentKindEnum.DOC_KIND_XCZPJSM.getCode());


    @Override
    public List<ConfigFastCaseMattersVo> fastProcedureList(ConfigFastProcedureDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getLength(), dto.getOrderBy());
        List<ConfigFastCaseMattersVo> list = configFastCaseMattersMapper.fastProcedureList(dto);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list;
    }

    @Override
    public Integer saveFastProcedure(ConfigFastCaseMattersDto dto) {
        ConfigFastCaseMatters configFastCaseMatters = new ConfigFastCaseMatters();
        BeanUtils.copyProperties(dto, configFastCaseMatters);
        AuditDataUtil.fillAuditAddData(configFastCaseMatters);
        configFastCaseMatters.setTemplateConfigStatus(Constant.STRING_1);
        configFastCaseMatters.setId(com.icinfo.framework.tools.utils.StringUtils.uuid());
        List<ConfigFastCaseMattersTemplate> configFastCaseMattersTemplateList = dto.getConfigFastCaseMattersTemplateList();
        for (ConfigFastCaseMattersTemplate caseMattersTemplate : configFastCaseMattersTemplateList) {
            ConfigFastCaseMattersTemplate insertTemplate = new ConfigFastCaseMattersTemplate();
            BeanUtils.copyProperties(caseMattersTemplate, insertTemplate);
            AuditDataUtil.fillAuditAddData(caseMattersTemplate);
            insertTemplate.setMattersConfigId(configFastCaseMatters.getId());
            insertTemplate.setId(com.icinfo.framework.tools.utils.StringUtils.uuid());
            //送达地址确认书 必填
            if (caseMattersTemplate.getDocumentKindCode().equals(DocumentKindEnum.DOC_KIND_SDDZQRS.getCode())) {
                insertTemplate.setIsRequired(Constant.STRING_1);
            }
            configFastCaseMattersTemplateMapper.insertSelective(insertTemplate);
        }
        return configFastCaseMattersMapper.insertSelective(configFastCaseMatters);
    }

    @Override
    public Integer updateFastProcedure(ConfigFastCaseMattersDto dto) {
        ConfigFastCaseMatters configFastCaseMatters = new ConfigFastCaseMatters();
        BeanUtils.copyProperties(dto, configFastCaseMatters);
        AuditDataUtil.fillAuditDataForUpdate(configFastCaseMatters);
        Example example = new Example(ConfigFastProcedure.class);
        example.createCriteria().andEqualTo("id", dto.getId());
        List<ConfigFastCaseMattersTemplate> configFastCaseMattersTemplateList = dto.getConfigFastCaseMattersTemplateList();
        for (ConfigFastCaseMattersTemplate caseMattersTemplate : configFastCaseMattersTemplateList) {
            AuditDataUtil.fillAuditAddData(caseMattersTemplate);
            caseMattersTemplate.setMattersConfigId(configFastCaseMatters.getId());
            //送达地址确认书 必填
            if (caseMattersTemplate.getDocumentKindCode().equals(DocumentKindEnum.DOC_KIND_SDDZQRS.getCode())) {
                caseMattersTemplate.setIsRequired(Constant.STRING_1);
            }
            configFastCaseMattersTemplateMapper.updateByPrimaryKeySelective(caseMattersTemplate);
        }
        return configFastCaseMattersMapper.updateByExampleSelective(configFastCaseMatters, example);
    }

    @Override
    public Integer deleteFastProcedure(ConfigFastProcedureDto dto) {
        ConfigFastCaseMatters configFastCaseMatters = new ConfigFastCaseMatters();
        configFastCaseMatters.setIsDeleted("1");
        Example example = new Example(ConfigFastCaseMatters.class);
        example.createCriteria().andEqualTo("id", dto.getId());
        return configFastCaseMattersMapper.updateByExampleSelective(configFastCaseMatters, example);
    }

    @Override
    public ConfigFastCaseMattersVo getFastProcedureDetail(ConfigFastProcedureDto dto) {
        ConfigFastCaseMattersVo mattersVo = new ConfigFastCaseMattersVo();
        ConfigFastCaseMatters configFastCaseMatters = configFastCaseMattersMapper.selectByPrimaryKey(dto.getId());
        BeanUtils.copyProperties(configFastCaseMatters, mattersVo);
        Example templateExample = new Example(ConfigFastCaseMattersTemplate.class);
        templateExample.createCriteria().andEqualTo("mattersConfigId", configFastCaseMatters.getId());
        templateExample.orderBy("sort").asc();
        List<ConfigFastCaseMattersTemplate> configFastCaseMattersTemplateList = configFastCaseMattersTemplateMapper.selectByExample(templateExample);
        mattersVo.setConfigFastCaseMattersTemplateList(configFastCaseMattersTemplateList);
        return mattersVo;
    }

    @Override
    public List<PunishmentMattersVo> getPunishmentMatters(String areaCode, String listName) {
        List<PunishmentMattersVo> list = new ArrayList<>();
        List<PunishmentMattersVo> punishmentMatters = configFastCaseMattersMapper.getPunishmentMatters(areaCode, listName);
        ConfigFastProcedureDto configFastProcedureDto = new ConfigFastProcedureDto();
        configFastProcedureDto.setAreaCode(areaCode);
        configFastProcedureDto.setListName(listName);
        List<ConfigFastCaseMattersVo> configFastProcedureVos = configFastCaseMattersMapper.fastProcedureList(configFastProcedureDto);
        if (!CollectionUtils.isEmpty(configFastProcedureVos)) {
            for (PunishmentMattersVo punishmentMatter : punishmentMatters) {
                boolean flag = validation(punishmentMatter, configFastProcedureVos);
                if (!flag) {
                    list.add(punishmentMatter);
                }
            }
        } else {
            return punishmentMatters;
        }
        return list;
    }

    @Override
    public List<ClaimOrgVo> getClaimOrg(String areaCode, String orgName) {
        return configFastCaseMattersMapper.getClaimOrg(areaCode, orgName);
    }

    @Override
    public ConfigFastCaseMattersVo getTemplateByListCode(String orgCode, String listCode) {
        return configFastCaseMattersMapper.getTemplateByListCode(orgCode, listCode);
    }

    @Override
    public List<ConfigFastCaseMattersVo> getListByOrgCode(String orgCode) {
        return configFastCaseMattersMapper.getListByOrgCode(orgCode);
    }

    @Override
    public int getFastProcedureNumByListCodeAndOrgCode(String orgCode, String listCode) {
        Example example = new Example(ConfigFastProcedure.class);
        example.createCriteria().andEqualTo("listCode", listCode)
                .andEqualTo("orgCode", orgCode)
                .andEqualTo("isDeleted", "0");
        return configFastCaseMattersMapper.selectCountByExample(example);
    }

    @Override
    public List<ConfigFastProcedureDocumentInfoVo> getDocumentTemplateByListCode(String orgCode, String listCode, String caseId) {
        CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(caseId);
        Example example = new Example(ConfigFastCaseMatters.class);
        example.createCriteria().andEqualTo("listCode", listCode)
                .andEqualTo("orgCode", orgCode)
                .andEqualTo("isDeleted", "0");
        example.orderBy("sort").asc();
        List<ConfigFastCaseMatters> configFastCaseMattersList = configFastCaseMattersMapper.selectByExample(example);
        List<ConfigFastProcedureDocumentInfoVo> documentInfoVoList = new ArrayList<>();
        if (ObjectUtil.isEmpty(configFastCaseMattersList) || configFastCaseMattersList.size() == 0) {
            return documentInfoVoList;
        }
        ConfigFastCaseMatters configFastCaseMatters = configFastCaseMattersList.get(0);

        Example templateExample = new Example(ConfigFastCaseMattersTemplate.class);
        templateExample.createCriteria().andEqualTo("mattersConfigId", configFastCaseMatters.getId()).andEqualTo("isShow", Constant.STRING_1);
        templateExample.orderBy("sort").asc();
        List<ConfigFastCaseMattersTemplate> configFastCaseMattersTemplateList = configFastCaseMattersTemplateMapper.selectByExample(templateExample);
        if (ObjectUtil.isNotEmpty(configFastCaseMattersTemplateList)) {
            for (ConfigFastCaseMattersTemplate caseMattersTemplate : configFastCaseMattersTemplateList) {
                ConfigFastProcedureDocumentInfoVo documentInfoVo = new ConfigFastProcedureDocumentInfoVo();
                BeanUtils.copyProperties(caseMattersTemplate, configFastCaseMatters);
                BeanUtils.copyProperties(caseMattersTemplate, documentInfoVo);
                ConfigBusiParamDocumentInfo configBusiParamDocumentInfo = configBusiParamDocumentInfoService.getFastCaseDocumentInfoByDocumentKindCode(caseMainInfo.getBusiParamId(), caseMattersTemplate.getDocumentKindCode(), Constant.STRING_1);
                if (ObjectUtil.isNotEmpty(configBusiParamDocumentInfo)) {
                    documentInfoVo.setDocumentCatalogCode(configBusiParamDocumentInfo.getDocumentCatalogCode());
                    documentInfoVo.setDocumentCatalogName(configBusiParamDocumentInfo.getDocumentCatalogName());
                    documentInfoVo.setIsConfirm(configBusiParamDocumentInfo.getIsConfirm());
                }
                String documentKindCode=documentInfoVo.getDocumentKindCode();
                //设置返回的操作按钮
                List<FastCaseEvidenceCollectOperateButtonInfoVo> operateButtonList = new ArrayList<>();
                List<String> operateButtonCodeList = new ArrayList<>();
                //在线制作
                if (!DocumentKindEnum.DOC_KIND_DK2QTWS.getCode().equals(documentInfoVo.getDocumentKindCode())) {
                    operateButtonList.add(new FastCaseEvidenceCollectOperateButtonInfoVo(FastCaseEvidenceCollectOperateButtonEnum.ONLINE_DOCUMENT.getCode(), FastCaseEvidenceCollectOperateButtonEnum.ONLINE_DOCUMENT.getDes()));
                    operateButtonCodeList.add(FastCaseEvidenceCollectOperateButtonEnum.ONLINE_DOCUMENT.getCode());
                }
                //关联文书
                if (DocumentKindEnum.DOC_KIND_SDDZQRS.getCode().equals(documentInfoVo.getDocumentKindCode()) || DocumentKindEnum.DOC_KIND_ZLGZTZS.getCode().equals(documentInfoVo.getDocumentKindCode())) {
                    operateButtonList.add(new FastCaseEvidenceCollectOperateButtonInfoVo(FastCaseEvidenceCollectOperateButtonEnum.RELATED_DOCUMENT.getCode(), FastCaseEvidenceCollectOperateButtonEnum.RELATED_DOCUMENT.getDes()));
                    operateButtonCodeList.add(FastCaseEvidenceCollectOperateButtonEnum.RELATED_DOCUMENT.getCode());
                }
                //线下上传
                if (!DocumentKindEnum.DOC_KIND_SDDZQRS.getCode().equals(documentInfoVo.getDocumentKindCode())) {
                    operateButtonList.add(new FastCaseEvidenceCollectOperateButtonInfoVo(FastCaseEvidenceCollectOperateButtonEnum.OFFLINE_DOCUMENT.getCode(), FastCaseEvidenceCollectOperateButtonEnum.OFFLINE_DOCUMENT.getDes()));
                    operateButtonCodeList.add(FastCaseEvidenceCollectOperateButtonEnum.OFFLINE_DOCUMENT.getCode());
                }
                documentInfoVo.setOperateButtonList(operateButtonList);
                documentInfoVo.setOperateButtonCodeList(operateButtonCodeList);
                documentInfoVo.setLinkType(EvidenceOperateTypeEnum.getLinkTypeByDocumentKindCode(caseMattersTemplate.getDocumentKindCode()));
                documentInfoVo.setDocumentShowName(caseMattersTemplate.getEvidenceAliasName());
                documentInfoVo.setDocumentKindCode(caseMattersTemplate.getDocumentKindCode());
                documentInfoVo.setDocumentKindName(caseMattersTemplate.getEvidenceName());
                documentInfoVo.setDocumentTemplateCode(caseMattersTemplate.getTemplateCode());
                documentInfoVo.setIsRequired(caseMattersTemplate.getIsRequired());
                documentInfoVo.setIsShow(caseMattersTemplate.getIsShow());
                documentInfoVo.setSort(FastCaseMattersTemplateTypeEnum.getSortByCode(caseMattersTemplate.getDocumentKindCode()));
                documentInfoVo.setTemplateSource(caseMattersTemplate.getTemplateSource());
                // todo 安吉速结特殊处理模板名称
                if(CaseTypeEnum.SIMPLE_QUICK.getCode().equals(caseMainInfo.getCaseType())){
                    if ( FAST_CASE_EVIDENCE_LIST.contains(documentKindCode)){
                        documentInfoVo.setDocumentShowName(caseMattersTemplate.getEvidenceName()+"（模板）");
                        //现场检查模板
                        if (DocumentKindEnum.DOC_KIND_DK2XCKYJCBL.getCode().equals(documentKindCode)){
                            documentInfoVo.setDocumentShowName("现场检查模板"+"（模板）");
                        }
                        if ( DocumentKindEnum.DOC_KIND_DK2XCZP.getCode().equals(documentKindCode)){
                            documentInfoVo.setDocumentShowName("现场照片"+"（模板）");
                        }
                        //勘验笔录
                        if (DocumentKindEnum.DOC_KIND_DK2KYBL.getCode().equals(documentKindCode)){
                            documentInfoVo.setDocumentShowName("勘验笔录"+"（模板）");
                        }
                        if (DocumentKindEnum.DOC_KIND_DK2XWBL.getCode().equals(documentKindCode)){
                            documentInfoVo.setDocumentShowName("询问笔录"+"（模板）");
                        }
                    }else {
                        documentInfoVo.setIsRequired(Constant.STRING_0);
                        documentInfoVo.setIsShow(Constant.STRING_0);
                    }
                }
                documentInfoVoList.add(documentInfoVo);
            }
        }
        return documentInfoVoList;
    }

    private boolean validation(PunishmentMattersVo punishmentMatter, List<ConfigFastCaseMattersVo> configFastProcedureVos) {
        for (ConfigFastCaseMattersVo configFastProcedureVo : configFastProcedureVos) {
            if (configFastProcedureVo.getListCode().equals(punishmentMatter.getListCode()) && configFastProcedureVo.getOrgCode().equals(punishmentMatter.getOrgCode())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 【V1.12.1】描述： 查询简案快办提示
     *
     * @return {@link String}
     * @author: zhengqiang
     * @date: 2023/8/17
     * @param: orgCode
     * @param: listCode
     **/
    @Override
    public String getFastTipsByListCodeAndOrgCode(String orgCode, String listCode) {
        ConfigFastCaseMattersVo configFastCaseMattersVo = getTemplateByListCode(orgCode, listCode);
        if (ObjectUtil.isNotEmpty(configFastCaseMattersVo)) {
            return configFastCaseMattersVo.getFastTip();
        }
        return null;
    }

    @Override
    public ConfigFastProcedureVo getTemplateByListCodeOld(String orgCode, String listCode) {
        ConfigFastProcedureVo configFastProcedureVo = new ConfigFastProcedureVo();
        ConfigFastCaseMattersVo configFastCaseMattersVo = configFastCaseMattersMapper.getTemplateByListCode(orgCode, listCode);
        if (ObjectUtil.isEmpty(configFastCaseMattersVo)){
            return null;
        }
        BeanUtils.copyProperties(configFastCaseMattersVo, configFastProcedureVo);
        Example templateExample = new Example(ConfigFastCaseMattersTemplate.class);
        templateExample.createCriteria().andEqualTo("mattersConfigId", configFastCaseMattersVo.getId());
        templateExample.orderBy("sort").asc();
        List<ConfigFastCaseMattersTemplate> configFastCaseMattersTemplateList = configFastCaseMattersTemplateMapper.selectByExample(templateExample);
        if (ObjectUtil.isNotEmpty(configFastCaseMattersTemplateList) && configFastCaseMattersTemplateList.size() > 0) {
            for (ConfigFastCaseMattersTemplate mattersTemplate : configFastCaseMattersTemplateList) {
                //现场检查模板
                if (DocumentKindEnum.DOC_KIND_DK2XCKYJCBL.getCode().equals(mattersTemplate.getDocumentKindCode())) {
                    configFastProcedureVo.setSiteInspectionTemplateName(mattersTemplate.getTemplateName());
                    configFastProcedureVo.setSiteInspectionTemplateCode(mattersTemplate.getTemplateCode());
                    configFastProcedureVo.setSiteInspectionIsRequired(mattersTemplate.getIsRequired());
                }
                //询问笔录改为调查询问笔录
                if (DocumentKindEnum.DOC_KIND_DK2XWBL.getCode().equals(mattersTemplate.getDocumentKindCode())) {
                    configFastProcedureVo.setRecordTemplateName(mattersTemplate.getTemplateName());
                    configFastProcedureVo.setRecordTemplateCode(mattersTemplate.getTemplateCode());
                    configFastProcedureVo.setRecordIsRequired(mattersTemplate.getIsRequired());
                }
                //身份证明
                if (DocumentKindEnum.DOC_KIND_DK2SFZM.getCode().equals(mattersTemplate.getDocumentKindCode())) {
                    configFastProcedureVo.setSfzmTemplateName(mattersTemplate.getTemplateName());
                    configFastProcedureVo.setSfzmTemplateCode(mattersTemplate.getTemplateCode());
                    configFastProcedureVo.setSfzmIsRequired(mattersTemplate.getIsRequired());
                }
                //现场示意图 改为 现场勘验图
                if (DocumentKindEnum.DOC_KIND_DK2XCKYT.getCode().equals(mattersTemplate.getDocumentKindCode())) {
                    configFastProcedureVo.setXcsytTemplateName(mattersTemplate.getTemplateName());
                    configFastProcedureVo.setXcsytTemplateCode(mattersTemplate.getTemplateCode());
                    configFastProcedureVo.setXcsytIsRequired(mattersTemplate.getIsRequired());
                }
                //现场照片
                if (DocumentKindEnum.DOC_KIND_DK2XCZP.getCode().equals(mattersTemplate.getDocumentKindCode())) {
                    configFastProcedureVo.setXczpjsmTemplateName(mattersTemplate.getTemplateName());
                    configFastProcedureVo.setXczpjsmTemplateCode(mattersTemplate.getTemplateCode());
                    configFastProcedureVo.setXczpjsmIsRequired(mattersTemplate.getIsRequired());
                }
                //勘验笔录改为 现场勘验笔录
                if (DocumentKindEnum.DOC_KIND_DK2KYBL.getCode().equals(mattersTemplate.getDocumentKindCode())) {
                    configFastProcedureVo.setKyblTemplateName(mattersTemplate.getTemplateName());
                    configFastProcedureVo.setKyblTemplateCode(mattersTemplate.getTemplateCode());
                    configFastProcedureVo.setKyblIsRequired(mattersTemplate.getIsRequired());
                }
            }
        }
        return configFastProcedureVo;
    }
}
